<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ResourceopController.php 310 2011-09-18 04:55:22Z beimuaihui@gmail.com $
 */



class System_OpinresourceController extends Baogg_Controller_Action {

	function init() {
		Zend_Loader::loadClass ( 'ResourceOp' );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;

		BAOGG_LANGUAGE::loadFile("resource");

		$params=$this->getRequest()->getParams();
		$form_url=$params;
		$form_url['action']='data';
		$edit_url=$params;
		$edit_url['action']='edit';
		$resource_id=$params['resource_id'];

		$Resource=new Resource();
		$rs_resource=$Resource->getOne($resource_id);
		$this->view->resource_name=$rs_resource[0]['resource_name'];
		
		$this->view->LANG=$LANG;

		$OpinResource=new OpInResource();
		$rs_op_in_resource=$OpinResource->getList(array('resource_id'=>$resource_id));
		$arr_op_in_resource=array();
		foreach((array)$rs_op_in_resource as $v){			
			$arr_op_in_resource[]=$v['resource_row_id'].'_'.$v['resource_op_id'];		
			
		}

		$this->view->arr_op_in_resource=$arr_op_in_resource;
		$this->view->form_url=Baogg_Controller_Url::generate($form_url);
		$this->view->edit_url=Baogg_Controller_Url::generate($edit_url);
		//echo '<pre>';var_dump($LANG);exit;


	}

	/**
	 * The data action
	 */
	function dataAction() {
		$post=$this->_request->getPost ();
		$resource_id=$this->_request->getParam('resource_id');
		/* $where=array();$order=array();$limit=array();
		foreach((array)$post as $k=>$v){
			if($k=="start" || $k=="limit"){
				$limit[$k]=$v;
			}else if($k=="sort" || $k=="dir"){
				$order[$k]=$v;
			}else{
				if($k=="op_in_resource_id"  ){
					if($v && is_numeric($v)){
						$v=(int)$v;
						$where[$k]=$v;
					}
				}else{
					$v='%'.$v.'%';
					$where[$k]=$v;
				}

			}
		}
		if(!$order){
			$order['sort']='ordering';
			$order['dir']='asc';
		}
		$where['resource_id']=(int)$resource_id; */
		$Resource=new Resource();
		$rs_resource=$Resource->getOne($resource_id);
		$filter=$rs_resource[0]['filter'];
		$sorting=$rs_resource[0]['sorting'];
		$arr_sorting=explode(" ",trim($sorting));
		$sort=null;
		if(count($arr_sorting)>=1){
			$arr_sorting=array_values(array_filter($arr_sorting));
			$sort=array('sort'=>$arr_sorting[0],'dir'=>(count($arr_sorting)>=2?$arr_sorting[1]:'asc'));
		}

		
		$ResourceOp=new ResourceOp();
		$rs_resource_op=$ResourceOp->getList(array('resource_id'=>$resource_id,'resource_op_pid'=>0),array('sort'=>'ordering','dir'=>'asc'),null,array('id2'=>'resource_op_id','text'=>'resource_op_name'));		
		foreach((array)$rs_resource_op as $k=>$v){
			$rs_resource_op[$k]['cls']='file';
			$rs_resource_op[$k]['leaf']=true;
			$rs_resource_op[$k]['checked']=false;
		}
		
		
		
		$Model=new Model();
		$arr_model=$Model->getModelAndField($rs_resource[0]['model_id']);
		/*$rs_model=$Model->getOne($rs_resource[0]['model_id']);
		
		$class_name=Model::tableNameToModel($rs_model[0]['table_name'],$rs_model[0]['db_key']);
		
		$ModelField=new ModelField();
		$rs_model_field=$ModelField->getList(array('model_id'=>$rs_resource[0]['model_id']));
		$key_col='';$label_col='';$parent_col='';
		foreach($rs_model_field as $v){
			if($v['field_key']=='PRI'){
				$key_col=$v['field_name'];
			}else if($v['is_label']){
				$label_col=$v['field_name'];
			}else if($v['is_parent']){
				$parent_col=$v['field_name'];
			}
		}*/
	
		$instance=new $arr_model['class_name']();
		
		if(isset($arr_model['parent_col'])){
			$rs=$instance->getExtTree($filter,$sort,null,array('id'=>$arr_model['key_col'],'text'=>$arr_model['label_col'],'pid'=>$arr_model['parent_col']),null,$rs_resource_op);
			//echo '<pre>';print_r($rs);exit;
			$rs=array(array('id'=>'0','text'=>$rs_resource[0]['resource_name'],'checked'=>false,'expanded'=>false, 'children'=>$rs));
		}else{
			$rs=$instance->getList($filter,null,null,array('id'=>$arr_model['key_col'],'text'=>$arr_model['label_col']));			
		}
		
		
		
		echo Zend_Json::encode($rs);
		$this->_helper->viewRenderer->setNoRender ();
	}


	/**
	 * The add action
	 */
	function addAction(){
		//$this->saveAction();
	}

	
	/**
	 * The delete action
	 */
	function deleteAction() {

		$ret=array("success"=>false,"msg"=>"delete_fail");
		$id = Baogg_Controller_Action::_getParam ( 'ids' );
			
		 

		if ($this->resourceOp->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}

		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();

	}

	function removeAction(){
		$this->deleteAction();
	}

	/**
	 * The save action
	 */
	function editAction() {
		//global $LANG;

		$ret=array("success"=>false,"msg"=>"save_fail");

		// echo '<pre>';print_r($this->resourceOp->info(ResourceOp::METADATA));exit;
		//check form
		if (!$this->_request->isPost ())
		{
			echo Baogg_Language::outputResult($ret);
			exit;
		}

		$formData = $this->_request->getPost ();

		if(!$formData)
		{
			echo Baogg_Language::outputResult($ret);
			exit;
		}
		
		$resource_id=$this->getRequest()->getParam('resource_id');
		$ids=$formData['ids'];
		$arr_id=array_filter(explode(',',$ids));
		
		//echo '<pre>';var_dump($ids);print_r($arr_id);exit;
		//first delete all
		$OpInResource=new OpInResource();
		$OpInResource->delete(array('resource_id='.(int)$resource_id));
		
		foreach((array)$arr_id as $v_id){
			$arr_v_id=explode('_',$v_id);
			//echo '<pre>';var_dump($arr_v_id);var_dump(count($arr_v_id)!=2  || !is_numeric($arr_v_id[0] );
			if(count($arr_v_id)!=2 || !is_numeric($arr_v_id[0]) || !is_numeric($arr_v_id[1])){
				continue;					
			}
			$OpInResource->_insert(array('resource_id'=>$resource_id,'resource_row_id'=>(int)$arr_v_id[0],'resource_op_id'=>(int)$arr_v_id[1]));
			
		}
				
		$ret=array("success"=>true,"msg"=>"save_ok");


		//$db=Zend_Registry::get("db");echo Baogg_Db::debugDb($db);exit;
		echo Baogg_Language::outputResult($ret);

		$this->_helper->viewRenderer->setNoRender ();


	}

}
